package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.DateUtil;
import ysq.xyg.base.util.EasyPoiUtil;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.sys.model.vo.SysDictContentVo;
import ysq.xyg.sys.service.SysDictContentService;
import ysq.xyg.web.dao.WebOrderDao;
import ysq.xyg.web.model.po.WebOrder;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.po.WebSignUp;
import ysq.xyg.web.model.vo.WebOrderChlidVo;
import ysq.xyg.web.model.vo.WebOrderVo;
import ysq.xyg.web.service.WebOrderService;
import ysq.xyg.web.service.WebSignUpService;
import ysq.xyg.web.util.WebBaseContent;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 业务模块-活动订单service实现
 *
 * @author shengQiang yu
 * @date 2019-5-11 10:02:08
 */
@Service("webOrderService")
public class WebOrderServiceImpl extends BaseServiceImpl<WebOrder> implements WebOrderService {
    @Resource
    private SysDictContentService sysDictContentService;
    @Resource
    private WebOrderDao webOrderDao;
    @Resource
    private WebSignUpService webSignUpService;
    private WebOrder webOrder;

    @Autowired
    public void setBaseDao(WebOrderDao webOrderDao) {
        this.baseDao = webOrderDao;
    }


    /**
     * 分页查询
     *
     * @param webOrderVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-5-11 10:02:08
     */
    public ReturnEntity page(WebOrderVo webOrderVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebOrder> list = acceptList(webOrderVo);
        PageInfo<WebOrder> page=new PageInfo<WebOrder>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webOrderVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-5-11 10:02:08
     */
    public ReturnEntity addOrUpdate(WebOrderVo webOrderVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webOrderVo.getId())){
            webOrder=new WebOrder(currentUser);
            isUpate=false;
        }else{
            webOrder=selectByPrimaryKey(webOrderVo.getId());
            if(ObjectUtil.isNull(webOrder)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webOrder.setUpdateTime(new Date());
            webOrder.setUpdateUser(currentUser.getId());
        }
        webOrder.setCheckTime(new Date());
        webOrder.setStatus(webOrderVo.getStatus());
        webOrder.setCheckRemark(webOrderVo.getCheckRemark());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webOrder);
        }else{
            num=insert(webOrder);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-5-11 10:02:08
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebOrder webOrder=selectByPrimaryKey(id);
        if(webOrder!=null){
            webOrder.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webOrder.setUpdateTime(new Date());
            webOrder.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webOrder);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }


    /**
     * 导出
     *
     * @param webOrderVo Vo对象
     * @author shengQiang yu
     * @date 2019-5-11 10:02:08
     */
    @Override
    public void export(HttpServletResponse response, WebOrderVo webOrderVo){
        WebSignUp webSignUp=webSignUpService.selectByPrimaryKey(webOrderVo.getSignId());
        String title=webSignUp.getTitle()+"人员";
        title=title+"_"+ DateUtil.getDateText(new Date(),"MMdd");
        if(WebBaseContent.WEB_USER_TYPE_CR.equals(webSignUp.getUserType())){
            // 导出操作
            List<WebOrder> list = acceptList(webOrderVo);
            // 导出操作
            EasyPoiUtil.exportExcel(list, title, "人员信息", WebOrder.class, title+".xls", response);
        }else{
            // 导出操作
            List<WebOrder> list = acceptList(webOrderVo);
            List<WebOrderChlidVo> webOrderChlidVoList=new ArrayList<WebOrderChlidVo>();
            List<SysDictContent> studentGradeDictList=sysDictContentService.acceptList(new SysDictContentVo(WebBaseContent.WEB_GRADE,BaseConstant.BASE_STATUS_NORMAL));
            List<SysDictContent> studentSchoolDictList=sysDictContentService.acceptList(new SysDictContentVo(WebBaseContent.WEB_SCHOOL,BaseConstant.BASE_STATUS_NORMAL));

            if(list!=null&&list.size()>0){
                for (WebOrder webOrder:list){
                    WebOrderChlidVo webOrderChlidVo=new WebOrderChlidVo();
                    webOrderChlidVo.setId(webOrder.getId());
                    webOrderChlidVo.setName(webOrder.getName());
                    webOrderChlidVo.setIdCard(webOrder.getIdCard());
                    webOrderChlidVo.setCheckTime(webOrder.getCheckTime());
                    webOrderChlidVo.setBrithday(webOrder.getBrithday());
                    webOrderChlidVo.setCheckRemark(webOrder.getCheckRemark());
                    webOrderChlidVo.setCreateTime(webOrder.getCreateTime());
                    webOrderChlidVo.setHomeAddress(webOrder.getHomeAddress());
                    webOrderChlidVo.setNation(webOrder.getNation());
                    webOrderChlidVo.setParentName(webOrder.getParentName());
                    webOrderChlidVo.setPhone(webOrder.getPhone());
                    webOrderChlidVo.setSex(webOrder.getSex());
                    webOrderChlidVo.setStatus(webOrder.getStatus());
                    SysDictContent studentGradeDictContent=sysDictContentService.accpetDictContent(webOrder.getStudentGrade(),studentGradeDictList);
                    if(studentGradeDictContent!=null){
                        webOrderChlidVo.setStudentGrade(studentGradeDictContent.getName());
                    }
                    SysDictContent studentSchoolDictContent=sysDictContentService.accpetDictContent(webOrder.getStudentSchool(),studentSchoolDictList);
                    if(studentSchoolDictContent!=null){
                        webOrderChlidVo.setStudentSchool(studentGradeDictContent.getName());
                    }
                    webOrderChlidVo.setWorkAddress(webOrder.getWorkAddress());
                    webOrderChlidVoList.add(webOrderChlidVo);
                }
            }


            // 导出操作
            EasyPoiUtil.exportExcel(webOrderChlidVoList, title, "人员信息", WebOrderChlidVo.class, title+".xls", response);
        }
    }

    /**
     * 获取集合
     *
     * @param webOrderVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-5-11 10:02:08
     */
    public List<WebOrder> acceptList(WebOrderVo webOrderVo){
        Example example = new Example(WebOrder.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webOrderVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webOrderVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(webOrderVo.getIdCard())){
            criteria.andEqualTo("idCard",webOrderVo.getIdCard());
        }
        if(StringUtil.isNotEmpty(webOrderVo.getMemberId())){
            criteria.andEqualTo("memberId",webOrderVo.getMemberId());
        }
        if(StringUtil.isNotEmpty(webOrderVo.getSignId())){
            criteria.andEqualTo("signId",webOrderVo.getSignId());
        }
        example.setOrderByClause(" update_time desc");
        return webOrderDao.selectByExample(example);
    }
}
